package com.li.utilstest

import android.hardware.display.DisplayManager
import android.hardware.display.VirtualDisplay
import android.media.MediaRecorder
import android.media.projection.MediaProjection
import android.os.Build
import androidx.annotation.RequiresApi

/**
 * @author : liyun
 * @ClassName: MediaRecordService
 * @Package com.li.utilstest
 * @Description:
 * @company: 深圳君南信息系统有限公司
 * @date 2021/8/22 18:02
 * @version v1.0
 */
class MediaRecordService(private val width: Int,
                         private val height: Int,
                         private val bitrate: Int,
                         private val dpi: Int,
                         private val mediaProjection: MediaProjection?,
                         private val destPath: String): Thread() {
    companion object {
        const val FRAME_RATE = 60
        const val TAG = "MediaRecordService"
    }

    private var mMediaRecorder: MediaRecorder?= null
    private var mVirtualDisplay: VirtualDisplay?= null

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun run() {
        super.run()
        initMediaRecorder()
        mVirtualDisplay = mediaProjection?.createVirtualDisplay(
            TAG,
            width,
            height,
            dpi,
            DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC,
            mMediaRecorder?.surface,
            null,
            null
        )
        mMediaRecorder?.start()
    }

    /**
     * 初始化MediaRecorder
     *
     * @return
     */
    private fun initMediaRecorder() {
        mMediaRecorder = MediaRecorder()
        mMediaRecorder?.apply {
            setVideoSource(MediaRecorder.VideoSource.SURFACE)
            setAudioSource(MediaRecorder.AudioSource.MIC) // 需要 Audio 权限，我们暂时不需要录音，只录屏
            setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
            setOutputFile(destPath)
            setVideoSize(width, height)
            setVideoFrameRate(FRAME_RATE)
            setVideoEncodingBitRate(bitrate)
            setVideoEncoder(MediaRecorder.VideoEncoder.H264)
            setAudioEncoder(MediaRecorder.AudioEncoder.AAC) // 需要 Audio 权限，我们暂时不需要录音，只录屏
            try {
                mMediaRecorder?.prepare()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    fun release() {
        if (mVirtualDisplay != null) {
            mVirtualDisplay?.release()
            mVirtualDisplay = null
        }
        if (mMediaRecorder != null) {
            mMediaRecorder?.setOnErrorListener(null)
            mediaProjection?.stop()
            mMediaRecorder?.reset()
            mMediaRecorder?.release()
        }
        mediaProjection?.stop()
    }
}